<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>片元着色器进行几何造型-小试牛刀</title>
    <style>
        canvas{
            border:1px solid #ccc;
        }
    </style>
</head>
<body>
    <canvas width="554" height="554"></canvas>

    <script src="/common/lib/gl-renderer.js"></script>

    <script type="module">
        import { grayMatrix } from "/review/common/lib/utils.js"

        /**
         * 1.创建renderer对象
         * 2.创建webgl程序
         * 3.创建uniform变量
         * 4.将数据存入缓冲区
         * 5.渲染
         * */ 

        //  1.
        const canvas = document.querySelector("canvas");
        const renderer = new GlRenderer(canvas);

        (async function(){
            // 2.
            const vertex = `
                attribute vec2 a_vertexPosition;
                attribute vec2 uv;
                varying vec2 vUv;

                void main(){
                    vUv = uv;
                    gl_Position = vec4(a_vertexPosition, 1.0, 1.0);
                }
            `;
            const fragment = `
                #ifdef GL_ES
                    precision mediump float;
                #endif

                uniform sampler2D tMap;
                uniform float uTime;

                varying vec2 vUv;

                void main(){
                    vec2 st = vUv - 0.5;
                    vec2 v = vec2(0, 1.0);

                    vec4 color = texture2D(tMap, vUv);

                    // 将时间的变化作为角度的变化
                    vec2 radius = vec2(sin(uTime), cos(uTime));

                    float a0 = cross(vec3(radius, 0.0), vec3(v, 0.0)).z;
                    float a1 = cross(vec3(st,0.0), vec3(v, 0.0)).z;
                    float a2 = cross(vec3(st,0.0), vec3(radius, 0.0)).z;

                    if(a0>0.0&&a1>0.0&&a2<0.0){
                        color.rg *= vec2(0.8, 1.4);
                    }
                    if(a0<0.0&&(a1>0.0||a2<0.0)){
                        color.rg *= vec2(0.8, 1.4);
                    }


                    gl_FragColor.rgb = color.rgb;
                    gl_FragColor.a = 1.0;
                }
            `;

            const program = renderer.compileSync(fragment,vertex);
            renderer.useProgram(program);

            // 3.
            const texture = await renderer.loadTexture("https://p4.ssl.qhimg.com/t011ce67c90abedcf12.jpg");

            renderer.uniforms.tMap = texture;
            renderer.uniforms.uTime = 0;
            
            // 4.
            renderer.setMeshData([{
                positions:[
                    [-1, -1],
                    [-1, 1],
                    [1, 1],
                    [1, -1],
                ],
                attributes:{
                    uv:[
                        [0, 0],
                        [0, 1],
                        [1, 1],
                        [1, 0],
                    ]
                },
                cells: [[0, 1, 2], [2, 0, 3]],
            }]);

            // 5.
            renderer.render();

            function update(t){
                renderer.uniforms.uTime = t * 0.002;
                requestAnimationFrame(update);
            }
            requestAnimationFrame(update);

        })();
        
    </script>
</body>
</html>